﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 建造者模式
{
    /// <summary>
    /// 产品类
    /// </summary>
    class Product
    {
        List<string> parts = new List<string>();
        public void Add(string part)
        {
            parts.Add(part);
        }
        public void Show()
        {
            Console.WriteLine("\n 产品 创建---");
           foreach (var p in parts )
           {
               Console.WriteLine( p );
           }
        }
    }

    //抽象建造者类(用于创建product对象的各个部分)
   abstract  class Builder
    {
        public abstract  void BuildPartA();
        public abstract void BuildPartB();
        public abstract Product GetResult();
    }
    //具体 建造者类（继承，具体如何创建这几个部分）
    class ConcreateBuilder1:Builder
    {
        Product product = new Product();
        public override void BuildPartA()
        {
            product.Add("部件A");
        }
        public override void BuildPartB()
        {
            product.Add("部件B");
        }
        public override Product GetResult()
        {
            return product;
        }
    }
    class ConcreateBuilder2 : Builder
    {
        Product product = new Product();
        public override void BuildPartA()
        {
            product.Add("部件X");
        }
        public override void BuildPartB()
        {
            product.Add("部件Y");
        }
        public override Product GetResult()
        {
            return product;
        }
    }
    //指挥官 类（根据用户的需要 ，建造所需的类（build子类））
    class Direcct
    {
        public void Construct(Builder build) 
        {
            build.BuildPartA();
            build.BuildPartB();
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            Builder b1 = new ConcreateBuilder1();
            Builder b2 = new ConcreateBuilder2();

            Direcct direct = new Direcct();
            direct.Construct(b1);
            direct.Construct(b2);   //创建，

          
            Product p1= b1.GetResult(); //必须放后面，只有创建了 才能显示
            p1.Show();                 //显示

           
            Product p2 = b2.GetResult();
            p2.Show();

           
            Console.ReadLine();
        }
    }
}
